package br.com.obardavez.guice;

import java.util.HashMap;
import java.util.Map;

import br.com.obardavez.entity.FileResource;
import br.com.obardavez.resources.ComentarioResource;
import br.com.obardavez.resources.ContatoResource;
import br.com.obardavez.resources.HistoriaResource;
import br.com.obardavez.resources.ParceiroResource;
import br.com.obardavez.resources.TesteResource;
import br.com.obardavez.resources.PromocaoResource;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.persist.PersistFilter;
import com.google.inject.persist.jpa.JpaPersistModule;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;

public class GuiceServletConfig extends GuiceServletContextListener {

	private static Injector injector;
	
	@Override
	protected Injector getInjector() {
		final Map<String, String> params = new HashMap<String, String>();
		
		params.put("javax.ws.rs.Application", "br.com.obardavez.web.filter.MainJerseyApplication");
		
		injector = Guice.createInjector(  new ServletModule() {
			@Override
			protected void configureServlets() {
				install(new JpaPersistModule("obardavezPU"));
				filter("/*").through(PersistFilter.class);
				bind(ContatoResource.class);
				bind(TesteResource.class);
				bind(ComentarioResource.class);
				bind(HistoriaResource.class);
				bind(FileResource.class);
				bind(ParceiroResource.class);
				bind(PromocaoResource.class);
				serve("/*").with(GuiceContainer.class, params);

				
				//PersistenceStrategy jpaEstatistica =JpaPersistenceStrategy.builder().unit("sidsEstatisticaJPAPU").annotatedWith(DataSourceEstatistica.class).build();
			//	install(PersistenceService.using(jpaEstatistica).across(UnitOfWork.TRANSACTION).buildModule());
				/*bind(ConsultaEnderecoResource.class);
				bind(ConsultaRapidaResource.class);
				bind(ProxyResource.class);
				bind(MapResource.class);
				bind(SelectionPropertyResource.class);
				bind(TranslationResource.class);
				bind(LoginResource.class);
				
				bind(BaseEntityManagerFinder.class).to(EntityManagerFinderImpl.class);
				
				serve("/rest/*").with(GuiceContainer.class, params);
				
				PersistenceStrategy jpaEstatistica =JpaPersistenceStrategy.builder().unit("sidsEstatisticaJPAPU").annotatedWith(DataSourceEstatistica.class).build();
				install(PersistenceService.using(jpaEstatistica).across(UnitOfWork.TRANSACTION).buildModule());
				
				PersistenceStrategy jpaMub =JpaPersistenceStrategy.builder().unit("sidsMubJPAPU").annotatedWith(DataSourceMub.class).build();
				install(PersistenceService.using(jpaMub).across(UnitOfWork.TRANSACTION).buildModule());
				
				PersistenceStrategy jpaCadMapa =JpaPersistenceStrategy.builder().unit("cadMapaJPAPU").annotatedWith(DataSourceCadMapa.class).build();
				install(PersistenceService.using(jpaCadMapa).across(UnitOfWork.TRANSACTION).buildModule());
				
				PersistenceStrategy jpaSecurity =JpaPersistenceStrategy.builder().unit("securityJPAPU").annotatedWith(DataSourceSecurity.class).build();
				install(PersistenceService.using(jpaSecurity).across(UnitOfWork.TRANSACTION).buildModule());*/
			}
		});
		
		return injector;
	}
	
	
	public static Injector getGuiceInjector() {
        return injector;
    }
}